package cn.angis.flow.controller;

import cn.angis.db.controller.FlowBaseController;
import cn.angis.db.model.PageModel;
import cn.angis.db.model.PageOfResult;
import cn.angis.common.model.R;
import cn.angis.db.controller.BaseController;
import org.dromara.warm.flow.core.FlowEngine;
import org.dromara.warm.flow.core.entity.Form;
import org.dromara.warm.flow.core.service.FormService;
import org.dromara.warm.flow.core.utils.page.Page;
import org.dromara.warm.flow.orm.entity.FlowForm;
import org.noear.solon.annotation.*;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.data.annotation.Transaction;

import java.util.List;

/**
 * 表单Controller
 *
 * @author liangli
 * @date 2024-09-05
 */
@Mapping("/flow/form")
@Controller
public class FormController extends FlowBaseController {

    @Inject
    private FormService formService;

    /**
     * 表单列表
     *
     * @param form 携带的查询参数
     * @return {@link R<PageOfResult<Form>>}
     * @author liangli
     * @date 2024/9/5 15:07
     **/
    @Mapping(value = "/list")
    @Get
    public R<PageOfResult<Form>> list(FlowForm form, PageModel pageModel) {
        Page<Form> page = Page.pageOf(pageModel.getCurrent(),pageModel.getSize());
        page = formService.orderByCreateTime().desc().page(form, page);

        PageOfResult<Form> pageOfResult = new PageOfResult<>();
        pageOfResult.setCurrent(pageModel.getCurrent());
        pageOfResult.setSize(pageModel.getSize());
        pageOfResult.setTotal(page.getTotal());
        pageOfResult.setRecords(page.getList());
        return success(pageOfResult);
    }

    /**
     * 已发布表单列表
     *
     * @param form 携带的查询参数
     * @return {@link R<PageOfResult<Form>>}
     * @author vanlin
     * @date 2024/11/11 15:07
     **/
    @Mapping(value = "/publishedList")
    @Get
    public R<PageOfResult<Form>> publishedList(FlowForm form, PageModel pageModel) {
        Page<Form> page = Page.pageOf(Integer.valueOf(pageModel.getCurrent()+""),pageModel.getSize());
        form.setIsPublish(1); //已发布
        page = formService.orderByCreateTime().desc().page(form, page);

        PageOfResult<Form> pageOfResult = new PageOfResult<>();
        pageOfResult.setCurrent(pageModel.getCurrent());
        pageOfResult.setSize(pageModel.getSize());
        pageOfResult.setTotal(page.getTotal());
        pageOfResult.setRecords(page.getList());
        return success(pageOfResult);
    }

    /**
     * 查询表单详情
     *
     * @param id 表单ID
     * @return {@link R< Form>}
     * @author liangli
     * @date 2024/9/5 15:41
     **/
    @Mapping(value = "/{id}")
    @Get
    public R<Form> getForm(Long id) {
        return success(formService.getById(id));
    }

    /**
     * 提交表单
     *
     * @param form 表单ID对应的表单内容
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 15:07
     **/
    @Mapping("/saveFormContent")
    @Post
    @Transaction
    public R<Boolean> saveFormContent(FlowForm form) {
        return success(formService.updateById(FlowEngine.newForm().setFormContent(form.getFormContent()).setId(form.getId())));
    }

    /**
     * 提交表单
     *
     * @param form 携带的查询参数
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 15:07
     **/
    @Mapping("/add")
    @Post
    @Transaction
    public R<Boolean> add(FlowForm form) {
        return success(formService.save(form));
    }

    /**
     * 发布表单
     *
     * @param id 表单ID
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 15:26
     **/
    @Mapping("/publish/{id}")
    @Get
    @Transaction
    public R<Boolean> publish(Long id) {
        return success(formService.publish(id));
    }

    /**
     * 取消表单
     *
     * @param id 表单ID
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 15:26
     **/
    @Mapping("/unPublish/{id}")
    @Get
    @Transaction
    public R<Boolean> unPublish(Long id) {
        return success(formService.unPublish(id));
    }

    /**
     * 修改表单
     *
     * @param form 表单
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 15:26
     **/
    @Mapping("/edit")
    @Put
    @Transaction
    public R<Boolean> edit(FlowForm form) {
        return success(formService.updateById(form));
    }

    /**
     * 删除表单
     *
     * @param ids 表单ID
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 16:19
     **/
    @Mapping("/{ids}")
    @Delete
    @Transaction
    public R<Boolean> remove(List<Long> ids) {
        return success(formService.removeByIds(ids));
    }

    /**
     * 复制表单
     *
     * @param id 表单ID
     * @return {@link R<Boolean>}
     * @author liangli
     * @date 2024/9/5 16:19
     **/
    @Mapping("/copyForm/{id}")
    @Post
    @Transaction
    public R<Boolean> copyForm(Long id) {
        return success(formService.copyForm(id));
    }
}
